<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;


class Index extends Controller
{
    // 首页
    public function index()
    {
    	$session = Session::get();

        // 一级分类
        $category = DB::table('category')->where('pid','0,')->select();


        // 图片
        $picture = DB::table(['picture' => 'p','hmy_user' => 'u'])
                    ->field('p.id,p.cate_id,p.title,p.src,u.icon,u.name,p.user_id')
                    ->where('u.id = p.user_id && p.status = 2')
                    ->order('upload_time')
                    ->select();

        // var_dump($picture);

        // 返回
        return view('index@index/index',[
            'session' => $session,
            'category' => $category,
            'picture' => $picture,
        ]);
    }


    // 列表
    public function lists($id)
    {
        $session = Session::get();

        // 一级分类
        $category = DB::table('category')->where('path','0,')->select();
        $ress = DB::table('category')->where('id',$id)->select();

        // 二级分类
        $a = '0,'.$id.',';
        $res = DB::table('category')->where('path',$a)->select();


        // 图片列表遍历
        $picture = DB::table(['picture' => 'p','hmy_user' => 'u'])
                    ->field('p.id,p.cate_id,p.title,p.src,u.icon,u.name,p.user_id')
                    ->where('u.id = p.user_id && p.cate_id = '.$id.'&& p.status = 2')
                    ->order('upload_time')
                    ->select();


        // var_dump($user_id);
        // var_dump($picture);die;


        return view('index@index/list',[
            'session' => $session,
            'category' => $category,
            'res' => $res,
            'ress' => $ress,
            'picture' => $picture
            // 'u_id' => $user_id
        ]);


    }



    // 三级分类
    public function sanji($path)
    {
        $session = Session::get();

        // 一级分类
        $category = DB::table('category')->where('path','0,')->select();


        // 二级分类
        $res = DB::table('category')->where('path',$path)->select();

        // 三级分类
        $c = DB::table('category')->where('path','like',$path.'%,')->select();

        // 图片列表遍历
        $picture = DB::table(['picture' => 'p','hmy_user' => 'u'])
                    ->where('u.id = p.user_id && p.cate_id = '.$res[0]['pid'].'&& p.status = 2')
                    ->order('upload_time')
                    ->select();


        return view('index@index/sanji',[
            'session' => $session,
            'res' => $res,
            'c' => $c,
            'category' => $category,
            'picture' => $picture
        ]);
    }


    // 详情
    public function detail($id)
    {
        $session = Session::get();

        // 一级分类
        $category = DB::table('category')->where('pid','0,')->select();

        $picture = DB::table(['picture' => 'p','hmy_user' => 'u','category' => 'c'])
                    ->field('p.id,p.cate_id,p.title,p.src,u.name,p.abstruct,u.icon')
                    ->where('u.id = p.user_id && p.cate_id = c.id && p.id = '.$id)
                    ->order('upload_time')
                    ->select();
        // var_dump($picture);die;

        $comment = DB::table(['picture' => 'p','comment' => 'c','hmy_user' => 'u'])
                    ->field('p.id,u.name,u.icon,c.content,c.commenttime,c.reply')
                    ->where('u.id = p.user_id && p.user_id = c.uid && p.id = '.$id)
                    ->order('upload_time')
                    ->select();

        // var_dump($comment);


        return view('index@index/detail',[
            'session' => $session,
            'category' => $category,
            'picture' => $picture[0],
            'comment' => $comment
        ]);
    }




     // 评论
    public function comment(Request $request)
    {

        // 接收数据
        $get = $request->get();

        // 获取用户id
        $session = Session::get();

        $id = $session['hmy_user']['id'];

        // 定义数据
        $data['uid'] = $id;
        $data['content'] = $get['content'];
        $data['pid'] = $get['id'];
        $data['name'] = $get['name'];
        $data['commenttime'] = date('Y-m-d H:i:s');


        // 插入sql
        $result = Db::table('comment')->data($data)->insert();

       // 跳转
       if ($result > 0) {

           $this->redirect('index/Index/detail',['id' => $get['id']]);

       }

    }

     // 回复评论
    public function reply(Request $request)
    {

        // 接收数据
        $data = $request->get();

        // 获取ID
        $session = Session::get();


        $id = $data['id'];


        // 插入sql
        $result = Db::table('comment')->where('id',$id)->update($data);

       // 跳转
       if ($result > 0) {

           $this->redirect('index/User/comment');

       }

    }



// 首页的模糊搜索
    public function online(Request $request)
    {
        $session = Session::get();
        $category = DB::table('category')->where('pid','0,')->select();

        // 接搜数据
        $data = $request->get();
        if ($data['name'] != null) {

            $a = $data['name'];
            $a = '%'.$a.'%';
            $res = Db::table(['category'=>'c'])
                        ->whereor('c.name','like',$a)
                        ->select();
        }else{
            $res = null;
        }

        return view('index@index/online',['session' => $session,'category' => $category,'res' => $res]);
    }




}

